package com.karry.service.impl;

import cn.hutool.core.util.StrUtil;
import com.karry.mapper.BlogMapper;
import com.karry.mapper.CommentMapper;
import com.karry.mapper.RecordMapper;
import com.karry.mapper.UserMapper;
import com.karry.pojo.Blog;
import com.karry.pojo.Comment;
import com.karry.pojo.User;
import com.karry.service.CommentService;
import com.karry.util.Page;
import com.karry.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.omg.CORBA.ARG_OUT;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CommentServiceImpl implements CommentService {

    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public ArrayList<Comment> searchBlogFatherComment(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<Comment> comments = mapper.searchBlogFatherComment(id);

        if (comments != null){
            //处理comment，加上昵称
            for (Comment comment : comments) {
                //获取发送用户昵称
                comment.setUserNick(userMapper.userSearchByID(comment.getUserId()).getNick());
                //头像
                comment.setUserHead(userMapper.userSearchByID(comment.getUserId()).getHead());
            }
        }
        sqlSession.close();
        return comments;
    }

    @Override
    public ArrayList<Comment> searchBackComment(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<Comment> comments = mapper.searchBackComment(id);
        //处理comment，加上昵称
        if (comments != null){
            //处理comment，加上昵称
            for (Comment comment : comments) {
                //获取发送用户昵称
                comment.setUserNick(userMapper.userSearchByID(comment.getUserId()).getNick());
                //头像
                comment.setUserHead(userMapper.userSearchByID(comment.getUserId()).getHead());
                //获取被回复用户昵称
                comment.setBackUserNick(userMapper.userSearchByID(comment.getBackUserId()).getNick());
            }
        }
        sqlSession.close();
        return comments;
    }

    @Override
    public Comment insertComment(int bID, int uID, String text, int cID, int bUID, String lText) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        Comment comment = new Comment();
        comment.setBlogId(bID);
        comment.setUserId(uID);
        comment.setText(text);
        comment.setFatherCommentId(cID);
        comment.setBackUserId(bUID);
        comment.setBackCommentText(lText);

        int id = mapper.insertComment(comment);
        System.out.println("id:" + comment.getId());
        comment = mapper.searchCommentByID(comment.getId());
        if (comment.getUserId() != 0){
            //获取自己的用户昵称
            comment.setUserNick(userMapper.userSearchByID(comment.getUserId()).getNick());
            //头像
            comment.setUserHead(userMapper.userSearchByID(comment.getUserId()).getHead());
        }
        //如果有父评论
        if (comment.getBackUserId() != 0){
            //获取父评论用户昵称
            comment.setBackUserNick(userMapper.userSearchByID(comment.getBackUserId()).getNick());
            System.out.println("父评论用户昵称：" + comment.getBackUserNick());
        }
        sqlSession.close();
        return comment;
    }

    @Override
    public ArrayList<Comment> searchNotify(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        ArrayList<Comment> comments = mapper.searchNotify(id);
        System.out.println("查询评论通知:" + comments);
        if (comments != null){
            //处理comment，加上昵称
            for (Comment comment : comments) {
                //获取发送用户昵称
                comment.setUserNick(userMapper.userSearchByID(comment.getUserId()).getNick());
                //头像
                comment.setUserHead(userMapper.userSearchByID(comment.getUserId()).getHead());
                //获取被回复用户昵称
                comment.setBackUserNick(userMapper.userSearchByID(comment.getBackUserId()).getNick());
            }
        }

        sqlSession.close();
        return comments;
    }

    @Override
    public int updateNotify(int bId, int bUId) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        int i = mapper.updateNotify(bId, bUId);
        sqlSession.close();
        return i;
    }

    @Override
    public Page<Comment> searchCommentByPage(String index, String size) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);

        // 设置分页参数的默认值
        Integer pageNum = 1; // 默认当前页是第一页
        Integer pageSize = 10; // 默认每页显示10条数据
        // 1. 参数的非空校验 （如果参数不为空，则设置参数）
        if (!StrUtil.isBlank(index)) {
            // 设置当前页
            pageNum = Integer.parseInt(index);
        }
        if (!StrUtil.isBlank(size)) {
            // 设置每页显示的数量
            pageSize = Integer.parseInt(size);
        }

        // 2. 查询当前登录用户的云记数量，返回总记录数 （long类型）
        long count = mapper.searchCommentSize();
        // 3. 判断总记录数是否大于0
        if (count < 1) {
            sqlSession.close();
            return null;
        }

        // 4. 如果总记录数大于0，调用Page类的带参构造，得到其他分页参数的值，返回Page对象
        Page<Comment> page = new Page<>(pageNum, pageSize, count);
        // 得到数据库中分页查询的开始下标
        Integer index_ = (pageNum -1) * pageSize;
        // 5. 查询当前登录用户下当前页的数据列表，返回note集合
        ArrayList<Comment> comments = mapper.searchCommentByPage(index_, pageSize);
        // 6. 将note集合设置到page对象中
        page.setDataList(comments);
        // 7. 返回Page对象
        sqlSession.close();
        return page;
    }

    @Override
    public int deleteComment(int id, HttpServletRequest request) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        RecordMapper record = sqlSession.getMapper(RecordMapper.class);
        int i = mapper.deleteComment(id);
        if (i > 0){
            User user = (User) request.getSession().getAttribute("user");
            //记录
            record.insertRecord("删除评论", user.getId(), -1, "评论id：" + id, "#");
        }
        sqlSession.close();
        return i;
    }

    @Override
    public int liveComment(int id, HttpServletRequest request) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);
        RecordMapper record = sqlSession.getMapper(RecordMapper.class);
        int i = mapper.liveComment(id);
        if (i > 0){
            User user = (User) request.getSession().getAttribute("user");
            //记录
            record.insertRecord("恢复评论", user.getId(), -1, "评论id：" + id, "#");
        }
        sqlSession.close();
        return i;
    }

    @Override
    public ArrayList<Comment> searchAllComment() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);

        ArrayList<Comment> comments = mapper.searchAllComment();
        sqlSession.close();
        return comments;
    }

    @Override
    public Comment searchFirstNotify(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);

        Comment comment = mapper.searchFirstNotify(id);
        System.out.println("通知查询：" + comment);
        sqlSession.close();
        return comment;
    }

    @Override
    public int updateNotify_(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        CommentMapper mapper = sqlSession.getMapper(CommentMapper.class);

        int i = mapper.updateNotifyByID(id);
        sqlSession.close();
        return i;
    }
}
